package com.boatAnchorAssistant.pojo.validation;

import com.boatAnchorAssistant.pojo.valid.Phone;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.springframework.util.StringUtils;

/**
 * @author Joseph.Li
 * @author joseph.li@king-v.com
 * Created in 2022-06-28 16:42
 * <p>
 * 电话号码检测
 */
public class PhoneValidation implements ConstraintValidator<Phone, String> {

    // 添加一个字段来存储正则表达式
    private String pattern;


    @Override
    public void initialize(Phone constraintAnnotation) {
        // 从注解中读取正则表达式
        this.pattern = constraintAnnotation.pattern();
    }

    @Override
    public boolean isValid(String nameField, ConstraintValidatorContext cxt) {
        if (!StringUtils.hasText(nameField)) {
            //为空不校验
            return true;
        }
        // add your own name validation here
        return nameField.matches(pattern);
    }
}
